/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-09-29     FuLei       the first version
 */
#ifndef MCU_HW_MYTIMER_C_
#define MCU_HW_MYTIMER_C_


#include <mytimer.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <mygpio.h>
#include <key_board.h>
#define HWTIMER_DEV_NAME   "timer2"     /* 定时器名称TIM2 */

/* 定时器超时回调函数 */
rt_err_t TIM2_timeout_callback(rt_device_t dev, rt_size_t size)
{
//    rt_kprintf("hwtimer2 cb, tick :%d !\n", rt_tick_get());
//    LED_Flash();

    //按键扫描核心函数
    key_check();
//    rt_kprintf("3\n");
    return 0;
}


int hwtimer_TIM2_Init(void)
{
    rt_err_t ret = RT_EOK;
    rt_hwtimerval_t timeout_s;      /* 定时器超时值 */
    rt_device_t hw_dev = RT_NULL;   /* 定时器设备句柄 */
    rt_hwtimer_mode_t mode;         /* 定时器模式 */
    rt_uint32_t freq = 100*1000;               /* 计数频率 */

    /* 1): 查找定时器设备 */
    hw_dev = rt_device_find(HWTIMER_DEV_NAME);
    if (hw_dev == RT_NULL)
    {
        rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_NAME);
        return RT_ERROR;
    }

    /* 2): 以读写方式打开设备 */
    ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
    if (ret != RT_EOK)
    {
        rt_kprintf("open %s device failed!\n", HWTIMER_DEV_NAME);
        return ret;
    }

    /* 3): 设置超时回调函数 */
    rt_device_set_rx_indicate(hw_dev, TIM2_timeout_callback);

    /* 4): 设置计数频率(若未设置该项，默认为1Mhz 或 支持的最小计数频率) */
    rt_device_control(hw_dev, HWTIMER_CTRL_FREQ_SET, &freq);    // 计数频率是 10kHz

    /* 5): 设置模式为周期性定时器（若未设置，默认是HWTIMER_MODE_ONESHOT）*/
    mode = HWTIMER_MODE_PERIOD;
    ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
    if (ret != RT_EOK)
    {
        rt_kprintf("set mode failed! ret is :%d\n", ret);
        return ret;
    }

    /// 设定定时周期！！！
    /* 6) 设置定时器超时值为5s并启动定时器 */
    timeout_s.sec = 0;      /* 秒 */
    timeout_s.usec = 1000;     /* 微秒 */
    if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
    {
        rt_kprintf("set timeout value failed\n");
        return RT_ERROR;
    }

    return ret;
}


//
///* 定时器超时回调函数 */
//static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
//{
//    rt_kprintf("this is hwtimer timeout callback fucntion!\n");
//    rt_kprintf("tick is :%d !\n", rt_tick_get());
//
//    return 0;
//}
//
//static int hwtimer_sample(int argc, char *argv[])
//{
//    rt_err_t ret = RT_EOK;
//    rt_hwtimerval_t timeout_s;      /* 定时器超时值 */
//    rt_device_t hw_dev = RT_NULL;   /* 定时器设备句柄 */
//    rt_hwtimer_mode_t mode;         /* 定时器模式 */
//    rt_uint32_t freq = 10000;               /* 计数频率 */
//
//    /* 查找定时器设备 */
//    hw_dev = rt_device_find(HWTIMER_DEV_NAME);
//    if (hw_dev == RT_NULL)
//    {
//        rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_NAME);
//        return RT_ERROR;
//    }
//
//    /* 以读写方式打开设备 */
//    ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
//    if (ret != RT_EOK)
//    {
//        rt_kprintf("open %s device failed!\n", HWTIMER_DEV_NAME);
//        return ret;
//    }
//
//    /* 设置超时回调函数 */
//    rt_device_set_rx_indicate(hw_dev, timeout_cb);
//
//    /* 设置计数频率(若未设置该项，默认为1Mhz 或 支持的最小计数频率) */
//    rt_device_control(hw_dev, HWTIMER_CTRL_FREQ_SET, &freq);
//    /* 设置模式为周期性定时器（若未设置，默认是HWTIMER_MODE_ONESHOT）*/
//    mode = HWTIMER_MODE_PERIOD;
//    ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
//    if (ret != RT_EOK)
//    {
//        rt_kprintf("set mode failed! ret is :%d\n", ret);
//        return ret;
//    }
//
//    /* 设置定时器超时值为5s并启动定时器 */
//    timeout_s.sec = 5;      /* 秒 */
//    timeout_s.usec = 0;     /* 微秒 */
//    if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
//    {
//        rt_kprintf("set timeout value failed\n");
//        return RT_ERROR;
//    }
//
//    /* 延时3500ms */
//    rt_thread_mdelay(3500);
//
//    /* 读取定时器当前值 */
//    rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s));
//    rt_kprintf("Read: Sec = %d, Usec = %d\n", timeout_s.sec, timeout_s.usec);
//
//    return ret;
//}
///* 导出到 msh 命令列表中 */
//MSH_CMD_EXPORT(hwtimer_sample, hwtimer sample);

#endif /* MCU_HW_MYTIMER_C_ */
